package com.example.Holo.rework.dao.implementation.sqlite.android;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.Holo.rework.core.Payment;
import com.example.Holo.rework.dao.interfaces.IPaymentDao;

import java.util.List;

import static com.example.Holo.rework.dao.implementation.sqlite.android.Column.*;
import static com.example.Holo.rework.dao.implementation.sqlite.android.ColumnDescription.createColumn;
import static com.example.Holo.rework.dao.implementation.sqlite.android.Nullable.NOT_NULL;
import static com.example.Holo.rework.dao.implementation.sqlite.android.PrimaryKey.primaryKey;
import static java.util.Arrays.asList;

public class PaymentDao extends GenericDao<Payment> implements IPaymentDao {

    protected PaymentDao(Context context, String databaseName, SQLiteDaoFactory daoFactory) {
        super(context, databaseName, daoFactory);
    }

    @Override
    protected String getTableName() {
        return "payments";
    }

    @Override
    protected List<ColumnDescription> getColumns() {
        return columnDescriptions;
    }

    @Override
    protected ContentValues getContentValues(Payment payment) {
        final ContentValues values = new ContentValues();
        values.put(EVENT_ID_COLUMN.name, payment.getEventId());
        values.put(PAID_PERSON_ID_COLUMN.name, payment.getPaidPersonId());
        values.put(NAME_COLUMN.name, payment.getName());
        values.put(QUANTITY_COLUMN.name, payment.getQuantity());
        return values;
    }

    @Override
    protected Payment createFromCursor(Cursor cursor) {
        return new Payment()
                .setId(cursor.getLong(cursor.getColumnIndex(ID_COLUMN.name)))
                .setPaidPersonId(cursor.getLong(cursor.getColumnIndex(PAID_PERSON_ID_COLUMN.name)))
                .setEventId(cursor.getLong(cursor.getColumnIndex(EVENT_ID_COLUMN.name)))
                .setName(cursor.getString(cursor.getColumnIndex(NAME_COLUMN.name)))
                .setQuantity(cursor.getDouble(cursor.getColumnIndex(QUANTITY_COLUMN.name)));
    }

    @Override
    protected Column getIdColumnName() {
        return ID_COLUMN;
    }

    private final List<ColumnDescription> columnDescriptions = asList(
            createColumn(ID_COLUMN, primaryKey(true, NOT_NULL)),
            createColumn(EVENT_ID_COLUMN),
            createColumn(PAID_PERSON_ID_COLUMN),
            createColumn(NAME_COLUMN),
            createColumn(QUANTITY_COLUMN)
    );

    @Override
    public long addPayment(Payment payment) {
        final SQLiteDatabase database = getWritableDatabase();
        final ContentValues contentValues = getContentValues(payment);
        return database.insert(getTableName(), null, contentValues);
    }

    @Override
    public void removePayment(long paymentId) {
        final SQLiteDatabase database = getWritableDatabase();
        final String whereClause = getIdColumnName() + " = " + paymentId;
        database.delete(getTableName(), whereClause, null);
    }

    @Override
    public Cursor getAllPayments(long eventId) {
        final SQLiteDatabase database = getWritableDatabase();
        return database.query(getTableName(), getAllColumns(), EVENT_ID_COLUMN.name + " = " + eventId, null, null, null, null);
    }

}
